// 数据劫持
class Hijack {
	constructor(vm) {
		this.notice = vm.$notice;
		this.init(vm.data);
	}
	init(data) {
		Object.keys(data).forEach(key => {
			let val = data[key];
			if (!val) return;
			if (typeof val === 'object') {
				return this.init(val);
			}
			Object.defineProperty(data, key, {
				configurable: true,
				get: () => {
					return val;
				},
				set: newVal => {
					if (val === newVal) return;
					val = newVal;
					if (typeof newVal === 'object') {
						return this.init(newVal);
					}
					this.notice.pub(newVal);
				}
			});
		});
	}
}

module.exports = Hijack;